<?php
/**
 * GioCMS
 * 
 * LICENSE
 *
 * This source file is subject to the GNU GENERAL PUBLIC LICENSE Version 2 
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.gnu.org/licenses/gpl-2.0.txt
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@ninhgio.com so we can send you a copy immediately.
 * 
 * @copyright	Copyright (c) 2010-2011 GioCMS (http://cms.ninhgio.com)
 * @license		http://www.gnu.org/licenses/gpl-2.0.txt GNU GENERAL PUBLIC LICENSE Version 2
 * @author		NinhGio - ninhgio@gmail.com
 * @since		1.0
 */
?>
<div class="g_upload_imagetoolbox">
	<div class="g_a_bottom">
		<?php echo sprintf($this->TRANSLATOR->widget('guide'), ini_get('upload_max_filesize')); ?>
	</div>
	
	<div>
		<center><input id="uploadImageToolbox_<?php echo $this->uploadElementId; ?>" name="uploadFile" type="file" /></center>
	</div>
	
	<div class="g_upload_imagetoolbox_images">
		<ul id="uploadImageToolboxThumbnails"></ul>
	</div>
</div>

<script type="text/javascript">
'Gio.Upload.Widgets.ImageToolbox'.namespace();

/**
 * Upload handlers
 */
Gio.Upload.Widgets.ImageToolbox.handleUploadComplete = function(event, ID, fileObj, response, data) {
	var images       = $.evalJSON(response);
	var presentImage = $('<img/>');
	
	$(presentImage).data('images', $.toJSON(images))
				   .attr('widget_data', $.toJSON(images))
				   .attr('src', images.thumbnail.url)
				   .attr('width', '90').attr('height', '90')
				   .attr('style', 'cursor: pointer')
				   .addClass('g_upload_imagetoolbox_draggable')
				   .draggable({
					   cursor: 'crosshair',
					   iframeFix: true,
					   helper: 'clone'
				   });
	
	var select = $('<select/>');
	$(select).attr('width', '100');
	$('<option/>').attr('value', '').html('---').appendTo(select);
	for (var thumb in images) {
		$('<option/>').data('images', $.toJSON(images))
					  .attr('widget_data', $.toJSON(images))
					  .attr('value', images[thumb].url)
					  .html(images[thumb].size)
					  .appendTo(select);
	}
	
	$(select).change(function() {
		if ($(this).val() != '') {
			$(presentImage).data('images', $(this).find('option:selected').data('images'))
						   .attr('widget_data', $(this).find('option:selected').data('images'))
						   .attr('src', $(this).val());
		}
	});

	$('<li/>').append(presentImage)
		      .append($('<br/>'))
		 	  .append(select)
			  .appendTo($('#uploadImageToolboxThumbnails'));
};

Gio.Upload.Widgets.ImageToolbox.handleDrop = function(droppedElement, event, ui) {
	if (!$(ui.helper).hasClass('g_upload_imagetoolbox_draggable')) {
		return;
	}
	var data = $(ui.helper).attr('widget_data');
	var src  = $(ui.helper).attr('src');
	data     = $.evalJSON(data);
	
	switch (true) {
		case ($(droppedElement).attr('type') == 'text' || $(droppedElement).attr('type') == 'textarea'):
			$(droppedElement).val($(this).val() + src);
			break;
		case ($(droppedElement).find('textarea.g_a_ui_editor').length > 0):
			/** Gets the Id of editor */
			var id      = $(droppedElement).find('textarea.g_a_ui_editor:first').attr('id');
			var factory = new Gio.Core.Editor.Factory();
			factory.factory('<?php echo $this->editor; ?>')
				   .setId(id)
				   .insertHtml('<img src="' + src + '" />');
			break;
		case ($(droppedElement).hasClass('g_a_upload_imagetoolbox_thumbnails')):
			var urls = {};
			for (var thumb in data) {
				urls[thumb] = data[thumb].url;
			}
			var hiddenElement = $(droppedElement).find('input[type="hidden"]').clone();
			$(hiddenElement).val($.toJSON(urls));
			$(droppedElement).css('height', 'auto')
						  .html('')
			       		  .append('<img src="' + data.crop.url + '" />')
			       		  .append($(hiddenElement));
			break;
	}
};

$(document).ready(function() {
	(new Gio.Upload.Uploader('uploadImageToolbox_<?php echo $this->uploadElementId; ?>'))
			.setUploadSettings({
				scriptData: {
					mod: MODULE,
					PHPSESSID: "<?php echo $this->STRING->escape($this->sessionId); ?>"
				},
				uploader: '<?php echo $this->APP_STATIC_SERVER; ?>/modules/upload/js/jquery.uploadify/uploadify.swf',
				script: '<?php echo $this->url('upload_file_upload'); ?>',
				cancelImg: '<?php echo $this->APP_STATIC_SERVER; ?>/modules/upload/js/jquery.uploadify/cancel.png',
				auto: true,
				multi: true,
				fileDesc: '*.jpg;*.jpeg;*.png;*.gif',
				fileExt: '*.jpg;*.jpeg;*.png;*.gif',
				onComplete: function(event, ID, fileObj, response, data) {
					Gio.Upload.Widgets.ImageToolbox.handleUploadComplete(event, ID, fileObj, response, data);
				}
			})
			.setMessageSettings({ sticky: true })
			.setButtonLabel('<?php echo $this->TRANSLATOR->translator('helper_uploader_upload_button', 'upload'); ?>')
			.show();
			
	/**
	 * Make the textbox, textarea, WYSIWYG editors droppable
	 */
	$([
		$('input[type="text"], textarea'),
		$('.g_a_ui_editor').parent(),
		$('.g_a_upload_imagetoolbox_thumbnails')
	]).each(function() {
		$(this).droppable({
			drop: function(event, ui) {
				Gio.Core.ToolBox.callDropHandler($(this), 'Gio.Upload.Widgets.ImageToolbox.handleDrop', event, ui);
			}
		});
	});
});
</script>